import { createRouter, createWebHistory } from 'vue-router';
import routes from '@/router/routes.ts';
import { storage } from '@/utils/LocalStorage.ts';

const router = createRouter({
  routes,
  history: createWebHistory(),
});

// 路由守卫
router.beforeEach(async (to, from, next) => {
  if (to.meta.title) {
    document.title = <string>to.meta.title;
  }
  if (
    to.path === '/login' ||
    to.name === 'teamInvite' ||
    to.name === 'boardInvite'
  ) {
    next();
  } else {
    let token = storage.get('satoken');
    if (token === null || token === '') {
      next('/login');
    } else {
      next();
    }
  }
});

export { router };
